import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;

/**
 * User: rmikhaylenko mirusnet@gmail.com
 * Date: 08/08/13
 * Time: 14:14
 */

public class PlaylistPanel extends JPanel {

    public PlaylistPanel() {
        ActionListener action = new ButtonAction();

        this.setLayout(new GridLayout(0, 3, 50, 50));
        this.setBackground(Color.DARK_GRAY);

        for (String playlist : PlaylistUtils.getPlaylists(Config.getMplayerConfigDir())) {
            displayButton(
                    Config.getMplayerIconDir() + PlaylistUtils.getIconName(playlist),
                    Config.getMplayerIconDir() + PlaylistUtils.getIconHoverName(PlaylistUtils.getIconName(playlist)),
                    playlist,
                    action);
        }

        for (int i = 0; i < Config.getMaximumPlaylistCount() - PlaylistUtils.getPlaylists(Config.getMplayerConfigDir()).size(); i++) {
            displayButton(
                    Config.getMplayerIconDir() + Config.getDefaultIconName(),
                    Config.getMplayerIconDir() + Config.getDefaultIconHoverName(),
                    Config.getDefaultAction(),
                    action);
        }
    }

    private void displayButton(String iconPath, String iconHoverPath, String playListPath, ActionListener action) {
        ImageIcon icon = new ImageIcon(iconPath);
        ImageIcon iconHover = new ImageIcon(iconHoverPath);
        JButton button = new JButton(icon);

        button.setRolloverIcon(iconHover);
        button.setBorderPainted(false);
        button.setFocusPainted(false);
        button.setContentAreaFilled(false);

        button.setActionCommand(playListPath);
        button.addActionListener(action);

        this.add(button);
    }
}
